<?php
namespace Extend\Controller;

/**
 * 同步到美住微预定相关rpc接口
 * @modify wenhui 2017-03-30 创建
 */
class BookSyncController extends ParentController {
    protected $refundStatusName = array(
        0 => '未申请/Not applied/未申請',
        1 => '申请退款/Apply for refund/申請退款',
        2 => '已退款/Refunded/已退款',
        3 => '已驳回/Rejected/已駁回',
    );

    protected function _initialize() {
       // $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * 美住微预定同步注册
     * @modify wenhui 2017-03-30 创建
     */
    public function webBookSyncRegister($data) {
        $userloginModel = D('UserLogin');

        $mobile = $data['mobile'];
        $password = $data['password'];
        $name = $data['name'];
        $areaCode = $data['areaCode'];
        $qrclientUserId = $data['qrclientUserId'];

        // 检查用户是否存在
        $userLogic = A('Common/User', 'Logic');
        $userInfo = $userLogic->checkUserExist($mobile, $areaCode);

        if ($userInfo == false) {
            $password = hash('sha256', $password);
            $registerResult = $userloginModel->register($mobile, $password, $name, $areaCode);
            if ($registerResult) {
                $userInfo = $userloginModel->getInfoByMobile($mobile, $areaCode);
            }
        }

        if (!empty($userInfo)) {
            return $userInfo;
        }
    }

     /**
     * 美住微预定同步注册
     * @modify wenhui 2017-03-30 创建
     */
    public function getwebBookData($data) {
        $userloginModel = D('UserLogin');
        $resultData = $userloginModel->getAllInfo();
        return $resultData;
    }

     /**
     * 把钥匙包用户全部注册
     * @modify wenhui 2017-03-30 创建
     */
    public function registerWebBook($data) {
        $userloginModel = D('UserLogin');

        $needRegist = $data['needRegist'];
        $userloginModel->addAllInfo($needRegist);
    }

     /**
     * 获取用户订单列表
     * @modify wenhui 2017-03-30 创建
     */
    public function userItem($data) {
        $webUserId = $data['webUserId'];
        $status = $data['status']; // 0:当前订单，1:历史订单
        $pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 1;
        $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 10;
        $qrLanguage = isset($data['qrLanguage']) ? $data['qrLanguage'] : 'zh-cn';

        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ORDER_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'userId' => $webUserId,
            'status' => $status,
            'qrLanguage' => $qrLanguage,
        );
        $result = $client->userItem($RPCData);

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            foreach ($result['data'] as $index => &$orderItem) {

                // 再次筛选
                foreach ($orderItem['orderRoom'] as $key => $orderRoomItems) {
                    $unset = false;
                    switch ($status) {
                        case 0:
                            $unset = in_array($orderRoomItems['status'], array(ORDER_BOOKED, ORDER_PAYED, ORDER_CHECKINED)) ? false : true;
                            break;
                        case 1:
                            $unset = !in_array($orderRoomItems['status'], array(ORDER_BOOKED, ORDER_PAYED, ORDER_CHECKINED)) ? false : true;
                            break;
                        case 2:
                            $unset = false;
                            break;
                        case 3:
                            $unset = in_array($orderRoomItems['status'], array(ORDER_BOOKED)) ? false : true;
                            break;
                        case 4:
                            $unset = in_array($orderRoomItems['status'], array(ORDER_PAYED)) ? false : true;
                            break;
                    }
                    if ($unset) {
                        unset($result['data'][$index]);
                    }
                }

                unset($orderItem['mobile']);
                unset($orderItem['name']);
                $orderItem['startDate'] = substr($orderItem['startDate'], 0, 10);
                $orderItem['endDate'] = substr($orderItem['endDate'], 0, 10);
                $orderItem['createtime'] = strtotime($orderItem['createtime']);
                $orderItem['orderDetailUrl'] = "http://" . MEIZHU_BOOK_HOST . "/Home/OrderPage/index?hotel={$orderItem['hotelentity_id']}&no={$orderItem['orderNumber']}&l={$qrLanguage}";
                // 筛选退款状态
                $unset = false;
                if (isset($data['refundStatus']) && $data['refundStatus'] == 5) {
                    $unset = in_array($orderItem['refundStatus'], array(ORDER_REFUND_APPLY, ORDER_REFUND_SUCCESS, ORDER_REFUND_REFUSE)) ? false : ture;
                }
                if ($unset) {
                    unset($result['data'][$index]);
                }

                if (isset($data['refundStatus']) && $data['refundStatus'] != 0) {
                    switch ($qrLanguage) {
                        case 'zh-cn':
                            $refundName = explode('/', $this->refundStatusName[$orderItem['refundStatus']])[0];
                            break;
                        case 'en-us':
                            $refundName = explode('/', $this->refundStatusName[$orderItem['refundStatus']])[1];
                            break;
                        case 'zh-tw':
                            $refundName = explode('/', $this->refundStatusName[$orderItem['refundStatus']])[2];
                            break;
                        default:
                            $refundName = explode('/', $this->refundStatusName[$orderItem['refundStatus']])[0];
                            break;
                    }
                    $orderItem['orderRoom'][0]['statusName'] = $refundName;
                }
            }
            unset($orderItem);
            /**
             *  按创建时间排序 
             *  SORT_ASC - 默认，按升序排列。(A-Z)
             *  SORT_DESC - 按降序排列。(Z-A)
             *  随后可以指定排序的类型：
             *  SORT_REGULAR - 默认。将每一项按常规顺序排列。
             *  SORT_NUMERIC - 将每一项按数字顺序排列。
             *  SORT_STRING - 将每一项按字母顺序排列
             */
            $result['data'] = my_sort($result['data'], 'createtime', SORT_DESC, SORT_NUMERIC);
            // 分页
            $result['data'] = orderCreatePageInfo($result['data'], $pageIndex, $pageSize);

            return array(
                'httpstatus' => $result['httpstatus'],
                'status' => 200,
                'order' => $result['data'],
            );
        }

        return array(
            'httpstatus' => $result['httpstatus'],
            'status' => $result['status'],
            'info' => $result['info']
        );
    }

     /**
     * 订单详情接口
     * @modify wenhui 2017-03-30 创建
     */
    public function detail($data) {
        $webUserId = $data['webUserId'];
        $orderNumber = $data['orderNumber'];

        if ($webUserId == 0) {
            return array(403, '没有权限进行此操作');
        }

        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ORDER_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'userId' => $webUserId,
            'orderNumber' => $orderNumber,
        );
        $result = $client->detail($RPCData);

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            $result['data']['startDate'] = substr($result['data']['startDate'], 0, 10);
            $result['data']['endDate'] = substr($result['data']['endDate'], 0, 10);
            $result['data']['user'] = $result['data']['user'];

            return array('httpstatus' => $result['httpstatus'], 'status' => 200,'detail' => $result['data']);
        }

        return array('httpstatus' => $result['httpstatus'], 'status' =>$result['status'] ,'info' => $result['info']);
    }

    /**
     * 获取用户房卡列表接口
     * @modify wenhui 2017-03-30 创建
     */
    public function cardItem($data) {
        $webUserId = $data['webUserId'];

        if ($webUserId == 0) {
            return array(403, 'card' => array());
        }

        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('CARD_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'userId' => $webUserId,
        );
        $result = $client->item($RPCData);
        if (!is_array($result)) {
            return array('httpstatus' => 500, 'status' => 500, 'info' => '服务器繁忙');
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            foreach ($result['data'] as &$cardItem) {
                $cardItem['startDate'] = substr($cardItem['startDate'], 0, 10);
                $cardItem['endDate'] = substr($cardItem['endDate'], 0, 10);
            }
            unset($cardItem);

            return array('httpstatus' => $result['httpstatus'], 'status' => 200,'card' => $result['data']);
        }

        return array('httpstatus' => $result['httpstatus'], 'status' =>$result['status'] ,'info' => $result['info']);
    }

    /**
     * 获取房卡数据接口
     * @modify wenhui 2017-03-30 创建
     */
    public function cardDetail($data) {

        // rpc 调用
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('CARD_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'bookUserId' => $data['bookUserId'],
            'orderRoomId' => $data['orderRoomId'],
            'hotel' => $data['hotel'],
            'userId' => $data['userId'],
        );
        $result = $client->detail($RPCData);
        if (!is_array($result)) {
            return array('httpstatus' => 500, 'status' => 500, 'info' => '服务器繁忙');
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            return array('status' => 200,'cardDetail' => $result['data']);
        }

        if (!empty($result['data'])) {
            return array('httpstatus' => $result['httpstatus'], 'status' =>$result['status'] ,'info' => $result['info'], 'data' => $result['data']);
        } else {
            return array('httpstatus' => $result['httpstatus'], 'status' =>$result['status'] ,'info' => $result['info']);
        }
    }

}
